package com.ftg.learn.chapter17.queue;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Vector;

/**
 * @author KangJx
 * @date 2021/8/6
 */
public class MyQueueImple<T> implements MyQueue<T> {

    /**
     * 底层的数组
     * 0就是现在的队首
     * obj.size()-1 队尾
     */
    private Vector<T> obj;

    /**
     * 默认情况下的队首
     */
    private int head = 0;

    private int count = 0;

    /**
     * 阈值
     */
    private int threshold = 20;

//    /**
//     * 默认情况下无参构造器初始的容器大小
//     */
//    private final int DEFAULT_CAPCITY = 10;

    public MyQueueImple(){
        this.obj = new Vector();
    }

    public MyQueueImple(int capcity){
        this.obj = new Vector(capcity);
    }

    public MyQueueImple(int capcity,int increment){
        this.obj = new Vector(capcity,increment);
    }

    /**
     * 不重复造轮子 动态数组 你可以扩容了
     * @param t1
     * @return
     */
    @Override
    public boolean add(T t1) {
        this.obj.add(t1);
        return true;
    }

    @Override
    public T peek() {
        if(this.obj.isEmpty()){
            return null;
        }
        return this.obj.get(head);
    }

    @Override
    public T element() {
        if(this.obj.isEmpty()){
            throw new NoSuchElementException("当前队列没有元素");
        }
        return this.obj.get(head);
    }

    @Override
    public T poll() {
        //删除
        if(head - this.obj.size() == 0){
            return null;
        }
        T t1 = this.obj.get(head++);
        //他不这么写
        if(count++ == threshold){
            for(int i = threshold;i >= 0;i--){
                this.obj.remove(i);
            }
            count = 0;
        }
        return t1;
    }


    //泛型擦除



}
